package definite.Guide.Book.ListModel;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

public class ModifyModelSamples {

	static String labels[] = { "Chardonnay", "Sauvignon", "Riesling",
		"Cabernet", "Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc",
		"Syrah", "Gewürztraminer" };

	public static void main(String[] args) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("JList Example");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				// declare model and fill
				final DefaultListModel model = new DefaultListModel();
				for (int i = 0; i < labels.length; i++)
					model.addElement(labels[i]);

				// JList
				JList list = new JList(model);
				JScrollPane scrollPane1 = new JScrollPane(list);
				frame.add(scrollPane1, BorderLayout.WEST);

				// JTextArea for results
				final JTextArea textArea = new JTextArea();
				textArea.setEditable(false);
				JScrollPane scrollPane2 = new JScrollPane(textArea);
				frame.add(scrollPane2, BorderLayout.CENTER);

				// define action
				ListDataListener listDataListener = new ListDataListener() {

					@Override
					public void intervalRemoved(ListDataEvent e) {
						appendEvent(e);
					}

					@Override
					public void intervalAdded(ListDataEvent e) {
						appendEvent(e);
					}

					@Override
					public void contentsChanged(ListDataEvent e) {
						appendEvent(e);
					}

					private void appendEvent(ListDataEvent e) {
						StringWriter sw = new StringWriter();
						PrintWriter pw = new PrintWriter(sw);
						switch (e.getType()) {
						case ListDataEvent.CONTENTS_CHANGED:
							pw.print("Type : contents_changed");
							break;
						case ListDataEvent.INTERVAL_ADDED:
							pw.print("Type : interval added");
							break;
						case ListDataEvent.INTERVAL_REMOVED:
							pw.print("Type : interval removed");
							break;
						}
						pw.print(", Index(0) " + e.getIndex0());
						pw.print(", Index(1) " + e.getIndex1());

						DefaultListModel listModel =
							(DefaultListModel) e.getSource();
						pw.println(listModel);
						textArea.append(sw.toString());
					}
				};
				model.addListDataListener(listDataListener);

				// setup button
				JPanel jp = new JPanel();
				jp.setLayout(new GridLayout(2, 1));
				JPanel jp1 =
					new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1));
				JPanel jp2 =
					new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1));
				jp.add(jp1);
				jp.add(jp2);

				JButton jb = new JButton("Add F");
				jp1.add(jb);
				jb.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						model.add(0, "First");
					}
				});

				jb = new JButton("AddElement L");
				jp1.add(jb);
				jb.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						model.addElement("Last");
					}
				});

				jb = new JButton("InsertElementAtM");
				jp1.add(jb);
				jb.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						int size = model.getSize();
						model.insertElementAt("Middle", size / 2);
					}
				});

				jb = new JButton("Set F");
				jp1.add(jb);
				jb.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						model.set(0, "New First");
					}
				});

				jb = new JButton("SetElementAtLast");
				jp1.add(jb);
				jb.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						int size = model.getSize();
						if (size != 0)
							model.setElementAt("New Last", size - 1);
					}
				});

				jb = new JButton("Load 10");
				jp1.add(jb);
				jb.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						for (int i = 0; i < labels.length; i++)
							model.addElement(labels[i]);
					}
				});

				jb = new JButton("Clear");
				jp2.add(jb);
				jb.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						model.clear();
					}
				});

				jb = new JButton("Remove F");
				jp2.add(jb);
				jb.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						int size = model.getSize();
						if (size != 0)
							model.remove(0);
					}
				});

				jb = new JButton("RemoveAllElems");
				jp2.add(jb);
				jb.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {

					}
				});
				frame.setSize(640, 300);
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(r);
	}
}
